package leetcode;

import java.util.Stack;

public class LeetCode20A {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        int length = s.length();
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '{' || c == '[')
                stack.push(c);
            else if (stack.size() == 0)
                return false;
            else if (c == ')' && stack.pop() != '(')
                return false;
            else if (c == '}' && stack.pop() != '{')
                return false;
            else if (c == ']' && stack.pop() != '[')
                return false;
        }
        return stack.size() == 0;
    }
//------------------------------------------------------------------
}
